﻿using System;
namespace Graphs
{
    public class Cycle
    {
        private bool[] marked;
        private bool hasCycle;

        public Cycle(Graph G)
        {
            marked = new bool[G.V()];
            for (var s = 0; s < G.V(); s++)
            {
                if (!marked[s])
                    Dfs(G,s,s);
            }
        }

        private void Dfs(Graph g, int v, int u)
        {
            marked[v] = true;
            foreach (var w in g.Adj(v))
            {
                if (!marked[w])
                    Dfs(g, w, v);
                else if (w != u)
                    hasCycle = true;
            }
        }

        public bool HasCycle()
        {
            return hasCycle;
        }
    }
}
